package uk.ac.tees.aetoolkit;

import java.text.DecimalFormat;
import java.util.ArrayList;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

/**
 * This class represents an alarms dialog activity which is displayed when the pending intent is recieved, it displays a dialog informing the user about the alarm.
 * 
 * @author Sohail Rahman, Mark Willamson, Robert Sheffield
 * 
 */
public class DialogActivity extends Activity {
 
	private AlertDialog.Builder mathDialog;
	private Vibrator v;
	private Ringtone r;
	private String operatorS;
	private String int1;
	private String int2;
	private boolean flag;
	private boolean weather;
	private boolean mathQ;

	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_dialogactivity);
		//gets the current intent.
		Intent intent = getIntent();
		//gets the extras from the intent.
		weather = intent.getBooleanExtra("Weather", false);
		mathQ = intent.getBooleanExtra("MathQ", false);
		ArrayList<String> operator = new ArrayList<String>();
		operator.add("+");
		operator.add("-");
		operator.add("/");
		operator.add("+");
		int operatorNum = 0 + (int) (Math.random() * ((3 - 0) + 1));
		operatorS = operator.get(operatorNum);
		DecimalFormat df = new DecimalFormat("#.#");
		int1 = df.format(Math.random());
		int2 = df.format(Math.random());
		final EditText input = new EditText(this);
		v = (Vibrator) this.getSystemService(this.VIBRATOR_SERVICE);
		long[] pattern = { 0, 200, 500 };
		v.vibrate(pattern, 0);
		Uri notification = RingtoneManager
				.getDefaultUri(RingtoneManager.TYPE_ALARM);
		r = RingtoneManager.getRingtone(this, notification);
		r.play();
		if (mathQ == true) {
			mathDialog = new AlertDialog.Builder(this);
			mathDialog.setTitle("Maths Question");
			mathDialog.setMessage("Answer this question to stop the alarm: "
					+ int1 + operatorS + int2);
			mathDialog.setView(input);
			flag = false;
			mathDialog.setPositiveButton("Answer",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							double answer = 0.00;
							if (operatorS.equals("+")) {
								answer = Double.parseDouble(int1)
										+ Double.parseDouble(int2);
							} else if (operatorS.equals("*")) {
								answer = Double.parseDouble(int1)
										* Double.parseDouble(int2);
							} else if (operatorS.equals("/")) {
								answer = Double.parseDouble(int1)
										/ Double.parseDouble(int2);
							} else if (operatorS.equals("-")) {
								answer = Double.parseDouble(int1)
										- Double.parseDouble(int2);
							}
							if (Double.parseDouble(input.getText().toString()) == (answer)) {
								Toast.makeText(getBaseContext(),
										"Thats correct you are now awake!",
										Toast.LENGTH_SHORT).show();
								flag = true;
								v.cancel();
								r.stop();
								Intent i = new Intent(DialogActivity.this,
										Alarm.class);
								startActivity(i);
								DialogActivity.this.finish();
							} else {
								v.cancel();
								r.stop();
								Toast.makeText(getBaseContext(),
										input.getText().toString(),
										Toast.LENGTH_SHORT).show();
								Intent alertIntent = new Intent(
										DialogActivity.this,
										DialogActivity.class);
								alertIntent.putExtra("MathQ", mathQ);
								startActivity(alertIntent);
							}
						}
					});
			mathDialog.setNegativeButton("SNOOZE", new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					// Exit application when they click 'OK'
					Toast.makeText(DialogActivity.this,
							"Snoozing for 1 minute", Toast.LENGTH_LONG).show();
					DialogActivity.this.finish();
				}
			});
			mathDialog.show();
		}
		if (mathQ == false) {
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
			alertDialog.setTitle("Alarm");
			alertDialog.setMessage("Wake Up");
			alertDialog.setPositiveButton("OK",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							// Exit application when they click 'OK'
							v.cancel();
							r.stop();
							Intent i = new Intent(DialogActivity.this,
									Weather.class);
							startActivity(i);
							DialogActivity.this.finish();
						}
					});
			alertDialog.setNegativeButton("SNOOZE", new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					// Exit application when they click 'OK'
					Toast.makeText(DialogActivity.this,
							"Snoozing for 1 minute", Toast.LENGTH_LONG).show();
					v.cancel();
					r.stop();
					Snooze();
					DialogActivity.this.finish();
				}
			});
			alertDialog.show();
		}
	}

	public void Snooze() {
		Intent i = new Intent(this, Alarm.class);
		// Creating a Pending Intent ,
		PendingIntent operation = PendingIntent.getActivity(
				DialogActivity.this, 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
		// Get reference to the ALARM_SERVICE,
		AlarmManager alarmManager = (AlarmManager) DialogActivity.this
				.getSystemService(DialogActivity.this.ALARM_SERVICE);
		// Convert the date and time in to milliseconds elapsed since epoch,
		long alarm_time = System.currentTimeMillis() + 1 * 60 * 1000;
		// Set an alarm which invokes the operation at alarm_time.
		alarmManager.set(AlarmManager.RTC_WAKEUP, alarm_time, operation);
	}
}